package com.jwaoo.mobile.view.register.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import com.dx.as.ToolBarOptions;
import com.dx.as.common.FragmentUtils;
import com.dx.as.common.ILoadListener;
import com.dx.as.fragment.TFragment;
import com.dx.as.listener.IImagePickListener;
import com.dx.as.upload.ProgressRequestBody;
import com.dx.as.upload.UploadFile;
import com.jwaoo.chat.session.actions.Container;
import com.jwaoo.chat.session.actions.ImageAction;
import com.jwaoo.media.dialog.CustomAlertDialog;
import com.jwaoo.mobile.R;
import com.jwaoo.mobile.view.register.IParamListener;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by jwaoo on 2017/10/16.
 */

public class HeadFragment extends TFragment{

    private IParamListener listener;
    private ImageView img_avatar;
    private ImageAction imageAction;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        listener = (IParamListener) context;
    }

    @Override
    public void initView() {
        img_avatar = findView(R.id.img_avatar);
        img_avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSelector();
            }
        });
        findView(R.id.btn_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            FragmentUtils.goToFragment(getActivity(),SexFragment.class,R.id.register_container);
            }
        });
        findView(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentUtils.goBackFragment(getActivity(),NickNameFragment.class,R.id.register_container,HeadFragment.class);
            }
        });
    }

    @Override
    public int setLayoutI() {
        return R.layout.fragment_head;
    }

    @Override
    public void dataOpion() {

    }

    @Override
    public void initRpo() {
        imageAction = new ImageAction(imagePickListener);
        imageAction.setContainer(new Container(getActivity(),0,null));
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void showSelector(){
        CustomAlertDialog dialog = new CustomAlertDialog(getActivity());
        dialog.setTitle(getString(com.jwaoo.media.R.string.input_panel_video));
        dialog.addItem("拍摄照片",new CustomAlertDialog.onSeparateItemClickListener(){
            @Override
            public void onClick() {
                imageAction.pickFromCamrera();
            }
        });
        dialog.addItem("从相册中选择照片",new CustomAlertDialog.onSeparateItemClickListener() {
            @Override
            public void onClick() {
                imageAction.pickFromLocal();
            }
        });
        dialog.show();
    }

    private IImagePickListener imagePickListener = new IImagePickListener() {
        @Override
        public void onPick(File file) {
            uploadFile(file);
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream);
                img_avatar.setImageBitmap(bitmap);
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        imageAction.onActivityResult(requestCode,resultCode,data);
    }

    private void uploadFile(File file){
        final Map<String,Object> parm = new HashMap<>();
        parm.put("as","pub");
        parm.put("flag","zoom");
        parm.put("type","img");
        parm.put("end",".jpg");
        parm.put("media_type","image/jpg");

        UploadFile.uploadFile(parm,file , new ILoadListener() {
            @Override
            public void onResponse(Object response, int flg) {
                if (response != null){
                    Map<String,String> result = (Map<String, String>) response;
                    String url = result.get("path");
                    listener.saveAvatarUrl(url);
                }
            }

            @Override
            public void onFailure(Throwable t) {
            }
        },new ProgressRequestBody(file,0,null));
    }
}
